﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using CommonUtility;
using System.Web.Script.Serialization;

namespace WebApplication
{
    public partial class ActionInf : BasePage
    {
        const string FunID = "FA05";
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Common.ToString(Request["Action"]);
            switch (action)
            {
                case "GetList":
                    Response.Write(LoadList());
                    Response.End();
                    break;
                case "Save":
                    Response.Write(DataSave());
                    Response.End();
                    break;
                case "Process":
                    Response.Write(Process(Common.ToString(Request["type"])));
                    Response.End();
                    break;
            }
            if (!IsPostBack && string.IsNullOrEmpty(action))
            {
                bllPermission Perm = new bllPermission(oUser.UserID, FunID);
                hdPermission.Value = new JavaScriptSerializer().Serialize(Perm);
            }
        }

        /// <summary>
        /// 获取明细
        /// </summary>
        /// <returns></returns>
        private string LoadList()
        {
            string result = "";
            result = new bllAction().GetActInf( Common.ConvertSql(Common.ToString(Request["ActID"])),Common.ConvertSql(Common.ToString(Request["ActName"])), oUser);
            return result;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        private string DataSave()
        {
            bool res = false;
            ActionInfo Act = new ActionInfo();
            if (new bllAction().CheckIfExist(Common.ToString(Request["RID"]), Common.ToString(Request["ActID"]), Common.ToString(Request["ActName"]), oUser).Count > 0)
            {
                return "existed";
            }
            Act.RID = string.IsNullOrEmpty(Common.ToString(Request["RID"])) ? 0 : Common.ToInt32(Request["RID"]);
            Act.ActID = Common.ToString(Request["ActID"]);
            Act.ActName = Common.ToString(Request["ActName"]);
            Act.Flag = Common.ToString(Request["Flag"]);
            Act.Lpt = DateTime.Now.ToString();
            Act.Whom = oUser.UserID;
            res = new bllAction().Add(FunID, Act, oUser);
            if (res)
            {
                return "successed";
            }
            else
            {
                return "failed";
            }
        }

        /// <summary>
        /// 设置Status
        /// </summary>
        /// <param name="_status">A or I</param>
        /// <returns></returns>
        private string Process(string _status)
        {
            string res = string.Empty;
            res = new bllAction().Process(FunID, Common.ToString(Request["rids"]), _status, oUser);
            return res;
        }
    }
}